<template>
	<view>
		<!-- 未认证 -->
		<view v-if="Dealer_Authority == ''">
			<!-- 账号 -->
			<view class="p-4 border-bottom border-light flex justify-between align-center">
				<text class="font-md font-weight-bold">账号:</text>
				<!-- <text v-if="!User_Number" class="font">{{User_Number}} &gt;</text> -->
				<input class="font" placeholder="请输入账号" v-model="User_Number" />
			</view>
			<!-- 密码 -->
			<view class="p-4 border-bottom border-light flex justify-between align-center">
				<text class="font-md font-weight-bold">密码:</text>
				<input class="font" type="password" placeholder="请输入密码" v-model="Pass_Word" />
			</view>
		</view>
		<!-- 已认证 -->
		<view v-else>
			<!-- 账号 -->
			<view class="p-4 border-bottom border-light flex justify-between align-center">
				<text class="font-md font-weight-bolder">绑定账号:</text>
				<text>{{ Dealer_Authority }}</text>
			</view>
			<!-- 密码 -->
			<view class="p-4 border-bottom border-light flex justify-between align-center">
				<text class="font-md font-weight-bolder">所属企业:</text>
				<text>{{ Enterprise_Name }}</text>
			</view>
		</view>
		<!--  -->
		<view>
			<!-- 提交 -->
			<view class="px-4 fixed-bottom flex align-center justify-center" style="height: 300rpx"
				v-if="Dealer_Authority == ''">
				<view class="bg-info rounded text-center py-2 text-white font-md font-weight-bold" style="width: 400rpx"
					@click="submit()" hover-class="bg-hover-info" :hover-stay-time="150">绑定商户</view>
			</view>
			<!-- 提交 -->
			<view class="px-4 fixed-bottom flex align-center justify-center" style="height: 300rpx"
				v-if="Dealer_Authority != ''">
				<!-- <view
          class="bg-info rounded text-center py-2 text-white font-md font-weight-bold"
          style="width: 200rpx"
          @click="Replace_binding()"
          hover-class="bg-hover-info"
          :hover-stay-time="150"
          >更换绑定</view
        > -->
				<view class="bg-info rounded text-center py-2 text-white font-md font-weight-bold" style="width: 200rpx"
					@click="unbind" hover-class="bg-hover-info" :hover-stay-time="150">解除绑定</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	import {
		WXDealerAuthen,
		WXEditAction,
		GoodsInquiryKeyOriginal,
		WeChatMember
	} from "@/api/global.js";
	export default {
		data() {
			return {
				User_Number: "",
				Pass_Word: "",
				loading: false,
				Dealer_Authority: "",
				Enterprise_Name: "",
				isShow: false,
			};
		},
		onLoad() {
			this.Dealer_Authority = app.globalData.memberInfo.Dealer_Authority;
			this.Enterprise_Name = app.globalData.memberInfo.Enterprise_Name;
			console.log(app.globalData.memberInfo);
		},
		methods: {
			async submit() {
				console.log(1);
				if (this.User_Number == "") {
					uni.showToast({
						title: "请输入账号",
						icon: "error"
					});
					return;
				}
				if (this.Pass_Word == "") {
					uni.showToast({
						title: "请输入密码",
						icon: "error"
					});
					return;
				}
				if (!this.loading) {
					this.loading = true;
					const res = await WXDealerAuthen({
						Enterprise_Info_SerialNumber: app.globalData.enterprise,
						User_Number: this.User_Number,
						Pass_Word: this.Pass_Word,
						Open_ID: app.globalData.openid,
						App_ID: app.globalData.appid,
					});
					if (res.Status == "success") {
						uni.showToast({
							title: "认证成功！",
							icon: "success"
						});
						this.WXMember(
							app.globalData.enterprise,
							app.globalData.appid,
							app.globalData.openid
						)
						const timer = setTimeout(() => {
							clearTimeout(timer);
							uni.navigateBack(); //后退
						}, 2000);
					} else if (res.Status == "fail1") {
						uni.showToast({
							title: res.Cause,
							icon: "error"
						});
					}
					this.loading = false;
				}
			},
			async WXMember(enterprise, App_ID, Open_ID) {
				//获取用户信息
				const res =  await WeChatMember({//个人信息查询
						enterprise,
						App_ID,
						Open_ID,
				})
				app.globalData.memberInfo = res[0];
			},
			async unbind() {
				uni.showModal({
					title: "提示",
					content: "是否解除绑定",
					success: function(res) {
						//5_2跳转到个人信息详情页
						if (res.confirm) {
							//跳转到页面
							GoodsInquiryKeyOriginal({
								Primary_Key_Name: "Member_SerialNumber",
								Primary_Key: app.globalData.memberInfo.Member_SerialNumber,
								Data: "member_info",
							}).then((res) => {
								console.log(res);
								res[0].Dealer_Authority = "";
								let data = {
									Data: JSON.stringify({
										Modify: {
											member_info: res
										},
									}),
									Open_ID: app.globalData.openid,
								};
							uni.showToast({
								title: "解除绑定成功！",
								icon: "success"
							});
							app.globalData.memberInfo.Dealer_Authority = "";
							const timer = setTimeout(() => {
								// clearTimeout(timer);
								uni.navigateBack(); //后退
							}, 2000);
								return WXEditAction(data);
							}).then((res2) => {
								console.log(res2); // 处理第二个接口的结果
							}).catch((error) => {
								console.error(error); // 错误处理
							});
						} else if (res.cancel) {
							//5_1关闭弹窗
							_this.$scope.globalData.memberInfo = _this.memberInfo;
						}
					},
				});
			},

			Replace_binding() {
				uni.navigateTo({
					url: "/pages/Merchant/Replace_binding",
				});
			},
		},
	};
</script>

<style>
</style>